package com.mzj.saas.mservice.sms.service;

import cn.hutool.core.util.StrUtil;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.mzj.saas.captcha.CaptchaController;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.commons.enums.SmsTemplateTypeEnum;
import com.mzj.saas.config.sms.SmsConfig;
import com.mzj.saas.redis.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 模板短信处理
 *
 * @author shun
 * @createDate: 2021年3月19日 下午5:40:19
 * @version: 1.0
 */
@Component
public class SmsService {
    private Logger LOG = LoggerFactory.getLogger(SmsService.class);

    @Autowired
    private SmsConfig smsConfig;
    @Autowired
    private CodeService codeService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CaptchaController captchaController;

    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;

    /**
     * 发送模板短信
     *
     * @param mobile       接收短信号码
     * @param code         验证码
     * @param templateType 模板类型1登录 2注册 3通用验证码短信
     * @return
     * @author shun
     * @date 2021年3月19日
     */
    public ResultBean<Object> sendTemplateCodeSms(String mobile, Integer templateType, String code, String captcha, String uuid) {
        LOG.info("SmsService sendLoginCodeTemplateSms, mobile={}, templateType={}, code={}", mobile, templateType, code);
        if (templateType == 1 || templateType == 2) {
            if (StrUtil.isEmpty(captcha) || StrUtil.isEmpty(uuid)) {
                return ResultBean.failedResult("参数不能为空");
            }
            captchaController.validateCaptcha(captcha, uuid);
        }

        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(smsConfig.getServerIp(), smsConfig.getServerPort());
        sdk.setAccount(smsConfig.getAccountSid(), smsConfig.getAccountToken());
        sdk.setAppId(smsConfig.getAppid());
        sdk.setBodyType(BodyType.Type_JSON);

        if (StringUtils.isEmpty(code)) {
            // 自动生成验证码
            code = codeService.getCode();
        }
        String[] datas = {code};
        String templateId = null;
        if (templateType == SmsTemplateTypeEnum.LOGIN.getValue()) {
            templateId = smsConfig.getLoginTemplateCode();
        } else if (templateType == SmsTemplateTypeEnum.REGISTER.getValue()) {
            templateId = smsConfig.getRegisterTemplateCode();
        } else {
            templateId = smsConfig.getGeneralTemplateCode();
        }
        HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, datas, null, null);
        if ("000000".equals(result.get("statusCode"))) {
            LOG.info("SmsService sendLoginCodeTemplateSms success, statusCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));

            // 有效时间五分钟
            redisService.putSmsCode(mobile, code);
            return ResultBean.successfulResult(result.get("statusCode"));
        } else {
            // 异常返回输出错误码和错误信息
            LOG.info("SmsService sendLoginCodeTemplateSms fail, errorCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));
            return ResultBean.failedResult(result.get("statusCode"));
        }
    }

    /**
     * 智能锁密码通知短信
     *
     * @param mobile
     * @param pwd
     * @return
     * @author shun
     * @date 2021年6月23日
     */
    public String sendTemplateMsgSms(String mobile, String pwd) {
        if (mobile.contains("_")) {
            mobile = mobile.substring(0, mobile.indexOf("_"));
        }
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(smsConfig.getServerIp(), smsConfig.getServerPort());
        sdk.setAccount(smsConfig.getAccountSid(), smsConfig.getAccountToken());
        sdk.setAppId(smsConfig.getAppid());
        sdk.setBodyType(BodyType.Type_JSON);

        String[] datas = {pwd};
        String templateId = smsConfig.getMsgTemplateCode();

        HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, datas, null, null);
        if ("000000".equals(result.get("statusCode"))) {
            LOG.info("SmsService sendTemplateMsgSms success, statusCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));

            return "success";
        } else {
            // 异常返回输出错误码和错误信息
            LOG.info("SmsService sendLoginCodeTemplateSms fail, errorCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));
            return "fail";
        }
    }

    /**
     * 资格审批不通过通知短信
     *
     * @param mobile
     * @return
     */
    public String sendTemplateNopassSms(String mobile) {
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(smsConfig.getServerIp(), smsConfig.getServerPort());
        sdk.setAccount(smsConfig.getAccountSid(), smsConfig.getAccountToken());
        sdk.setAppId(smsConfig.getAppid());
        sdk.setBodyType(BodyType.Type_JSON);

        String templateId = smsConfig.getNopassTemplateCode();

        HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, null, null, null);
        if ("000000".equals(result.get("statusCode"))) {
            LOG.info("SmsService sendTemplateNopassSms success, statusCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));

            return "success";
        } else {
            // 异常返回输出错误码和错误信息
            LOG.info("SmsService sendTemplateNopassSms fail, errorCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));
            return "fail";
        }
    }


    public String sendTemplateSms(String mobile, String templateId) {
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(smsConfig.getServerIp(), smsConfig.getServerPort());
        sdk.setAccount(smsConfig.getAccountSid(), smsConfig.getAccountToken());
        sdk.setAppId(smsConfig.getAppid());
        sdk.setBodyType(BodyType.Type_JSON);

        // String templateId = smsConfig.getNopassTemplateCode();

        HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, null, null, null);
        if ("000000".equals(result.get("statusCode"))) {
            LOG.info("SmsService sendTemplateNopassSms success, statusCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));

            return "success";
        } else {
            // 异常返回输出错误码和错误信息
            LOG.info("SmsService sendTemplateNopassSms fail, errorCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));
            return "fail";
        }
    }

    public String sendTemplateSmsWithData(String mobile, String templateId, String[] datas) {
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(smsConfig.getServerIp(), smsConfig.getServerPort());
        sdk.setAccount(smsConfig.getAccountSid(), smsConfig.getAccountToken());
        sdk.setAppId(smsConfig.getAppid());
        sdk.setBodyType(BodyType.Type_JSON);

        // String templateId = smsConfig.getNopassTemplateCode();
        HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, datas, null, null);
        if ("000000".equals(result.get("statusCode"))) {
            LOG.info("SmsService sendTemplateSmsWithData success, statusCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));

            return "success";
        } else {
            // 异常返回输出错误码和错误信息
            LOG.info("SmsService sendTemplateSmsWithData fail, errorCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));
            return "fail";
        }
    }


}
